﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace Schedulr.Views.Dialogs
{
    /// <summary>
    /// Interaction logic for AuthenticationDialog.xaml
    /// </summary>
    public partial class AuthenticationDialog : Window
    {
        /// <summary>
        /// The Flickr authentication URL.
        /// </summary>
        private string authenticationUrl;

        /// <summary>
        /// The Flickr verifier code.
        /// </summary>
        public string Verifier { get { return this.verifierTextBox.Text.Trim(); } }

        public AuthenticationDialog(string authenticationUrl)
            : this(authenticationUrl, null)
        {
        }

        public AuthenticationDialog(string authenticationUrl, string infoText)
        {
            InitializeComponent();
            this.Owner = App.Instance.MainWindow;
            this.authenticationUrl = authenticationUrl;
            this.authenticationWebBrowser.Navigate(this.authenticationUrl);
            if (string.IsNullOrEmpty(infoText))
            {
                infoText = "Before you continue, you must authenticate with Flickr and authorize this application to access your account. When done, enter the verifier code in the text box below.";
            }
            this.infoTextBlock.Text = infoText;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void authenticationHyperlink_Click(object sender, RoutedEventArgs e)
        {
            Process.Start(this.authenticationUrl);
        }

        private void verifierTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.okButton.IsEnabled = !string.IsNullOrWhiteSpace(this.verifierTextBox.Text);
        }
    }
}